#include "usart.h"
#include "main.h"
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
//extern UART_HandleTypeDef huart3;
//extern UART_HandleTypeDef huart4;
//extern UART_HandleTypeDef huart5;
//extern UART_HandleTypeDef huart6;
//************************UART1********************************
#ifdef _UART1_ENABLE_
USART_INFO_STRUCT U1S;
char U1S_RX_BUFFER[USART1_RX_DATA_MAX + 1];
#ifdef _UART1_DMA_
extern DMA_HandleTypeDef hdma_usart1_rx;
static char	U1S_DMA_BUFFER[USART1_RX_DATA_MAX + 1];
#else
static USART_DELAY_STRUCT U1T;
#endif

void USART1_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart1, (u8 *)&Byte, 1, 0xffff);
}
void USART1_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart1, (u8 *)iStr, strlen(iStr), 0xffff);

}
void USART1_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart1, (u8 *)iPrt, Len, 0xffff);
}
void USART1_ReciveReset(void)
{
    U1S.RxLen = 0;
    memset(U1S.RxBuffer, 0, USART1_RX_DATA_MAX);
}
static void USART1_CallBackTIM()
{
#ifndef _UART1_DMA_
    if(U1S.RxSate == STA_BUSY)
    {
        if(++U1T.Delay > U1T.DelayMax)
        {
            U1S.RxSate = STA_END;
            U1S.RxLen = U1S.DmaLen;
            U1S.DmaLen = 0;
        }
    }
#endif
}
//********************************************************************************
void USART1_InterruptCall(void)
{
#ifdef _UART1_DMA_
    if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart1);
    HAL_UART_DMAStop(&huart1);
    u32 temp1 = hdma_usart1_rx.Instance->NDTR;
    U1S.DmaLen = USART1_RX_DATA_MAX - temp1;
    if(U1S.DmaLen == 0)return;
    if(U1S.RxLen != 0)return;
    if(U1S.RxBuffer == NULL)return;
    memcpy(U1S.RxBuffer, U1S_DMA_BUFFER, U1S.DmaLen);
    U1S.RxLen = U1S.DmaLen;
    memset(U1S_DMA_BUFFER, 0x00, USART1_RX_DATA_MAX);
    U1S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart1, (u8 *)U1S_DMA_BUFFER, USART1_RX_DATA_MAX);
//	printf("\r\n RxLen=%d,Data=%s",U1S.RxLen,U1S.RxBuffer);
#else
    u8 bytes = (u8)(huart1.Instance->RDR);
    U1S.RxSate = STA_BUSY;
    if(U1T.Busy == 0) U1S.RxBuffer[U1S.DmaLen++] = bytes;
//		printf("\r\n%d,%d,%c",U1T.Busy,U1S.DmaLen,bytes);
    if(U1S.DmaLen > USART1_RX_DATA_MAX)
    {
        U1S.DmaLen = 0;
        U1S.RxSate = STA_OVER;
        printf("\r\n USART1 SPILL");
    }
    U1T.Delay = 0;
#endif
}
#endif

#ifdef _UART2_ENABLE_
//************************UART2********************************
USART_INFO_STRUCT U2S;
char U2S_RX_BUFFER[USART2_RX_DATA_MAX + 1];
#ifdef _UART2_DMA_
extern DMA_HandleTypeDef hdma_usart2_rx;
static char	U2S_DMA_BUFFER[USART2_RX_DATA_MAX + 1];
#else
static USART_DELAY_STRUCT U2T;
#endif
void USART2_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart2, (uint8_t *)&Byte, 1, 0xffff);
}
void USART2_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart2, (uint8_t *)iStr, strlen(iStr), 0xffff);
}
void USART2_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart2, iPrt, Len, 0xffff);
}
void USART2_ReciveReset(void)
{
    U2S.RxLen = 0;
    memset(U2S.RxBuffer, 0, USART2_RX_DATA_MAX);
}
void USART2_InterruptCall(void)
{
#ifdef _UART2_DMA_
    if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart2);
    HAL_UART_DMAStop(&huart2);
    u32 temp1 = hdma_usart2_rx.Instance->CNDTR;
    U2S.DmaLen = USART2_RX_DATA_MAX - temp1;
		printf("\r\n CNDTR=%d,DmaLen=%d,",temp1,U2S.DmaLen);
    if(U2S.DmaLen == 0)return;
    if(U2S.RxLen != 0)return;
    if(U2S.RxBuffer == NULL)return;
    memcpy(U2S.RxBuffer, U2S_DMA_BUFFER, U2S.DmaLen);
    U2S.RxLen = U2S.DmaLen;
    memset(U2S_DMA_BUFFER, 0x00, USART2_RX_DATA_MAX);
    U2S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart2, (u8 *)U2S_DMA_BUFFER, USART2_RX_DATA_MAX);
#else
    u8 bytes = (u8)(huart2.Instance->RDR);
    U2S.RxSate = STA_BUSY;
//		printf("%c",huart2.Instance->RDR);
    U2S.RxBuffer[U2S.DmaLen++] = bytes;
    if(U2S.DmaLen > USART2_RX_DATA_MAX)
    {
        printf("\r\n USART2 SPILL %s",U2S.RxBuffer);
        U2S.DmaLen = U2T.Busy = 0;
        U2S.RxSate = STA_OVER;
    }
    U2T.Delay = 5;
#endif
}

static void USART2_CallBackTIM()
{
#ifndef _UART2_DMA_
    if(U2S.RxSate == STA_BUSY)
    {
        if(++U2T.Delay > U2T.DelayMax)
        {
            U2S.RxSate = STA_END;
            U2S.RxLen = U2S.DmaLen;
            U2S.DmaLen = 0;
        }
    }
#endif
}
#endif

#ifdef _UART3_ENABLE_
//************************UART3********************************
USART_INFO_STRUCT U3S;
char U3S_RX_BUFFER[USART3_RX_DATA_MAX + 1];
#ifdef _UART3_DMA_
static char	U3S_DMA_BUFFER[USART3_RX_DATA_MAX + 1];
extern DMA_HandleTypeDef hdma_usart3_rx;
#else
static USART_DELAY_STRUCT U3T;
#endif
void USART3_InterruptCall(void)
{
#ifdef _UART3_DMA_
    if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart3);
    HAL_UART_DMAStop(&huart3);
    u32 temp1 = hdma_usart3_rx.Instance->NDTR;
    U3S.DmaLen = USART3_RX_DATA_MAX - temp1;
    if(U3S.DmaLen == 0)return;
    if(U3S.RxLen != 0)return;
    if(U3S.RxBuffer == NULL)return;
    memcpy(U3S.RxBuffer, U3S_DMA_BUFFER, U3S.DmaLen);
    U3S.RxLen = U3S.DmaLen;
    memset(U3S_DMA_BUFFER, 0x00, USART3_RX_DATA_MAX);
    U3S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart3, (u8 *)U3S_DMA_BUFFER, USART3_RX_DATA_MAX);
#else
    u8 bytes = (u8)(huart3.Instance->DR);
    U3S.RxSate = STA_BUSY;
    if(U3T.Busy == 0) U3S.RxBuffer[U3S.DmaLen++] = bytes;
    if(U3S.DmaLen > USART3_RX_DATA_MAX)
    {
        U3S.DmaLen = 0;
        U3S.RxSate = STA_OVER;
        printf("\r\n USART3 SPILL");
    }
    U3T.Delay = 0;
#endif
}
void USART3_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart3, (uint8_t *)&Byte, 1, 0xffff);
}
void USART3_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart3, (uint8_t *)iStr, strlen(iStr), 0xffff);
}
void USART3_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart3, iPrt, Len, 0xffff);
}
void USART3_ReciveReset(void)
{
    U3S.RxLen = 0;
    memset(U3S.RxBuffer, 0, USART3_RX_DATA_MAX);
}
static void USART3_CallBackTIM()
{
#ifndef _UART3_DMA_
    if(U3S.RxSate == STA_BUSY)
    {
        if(++U3T.Delay > U3T.DelayMax)
        {
            U3S.RxSate = STA_END;
            U3S.RxLen = U3S.DmaLen;
            U3S.DmaLen = 0;
        }
    }
#endif
}
#endif

#ifdef _UART4_ENABLE_
//************************UART4********************************
USART_INFO_STRUCT U4S;
char U4S_RX_BUFFER[USART4_RX_DATA_MAX + 1];
#ifdef _UART4_DMA_
static char	U4S_DMA_BUFFER[USART4_RX_DATA_MAX + 1];
extern DMA_HandleTypeDef hdma_uart4_rx;
#else
static USART_DELAY_STRUCT U4T;
#endif
void USART4_InterruptCall(void)
{
#ifdef _UART4_DMA_
    if(__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart4);
    HAL_UART_DMAStop(&huart4);
    u32 temp1 = hdma_uart4_rx.Instance->NDTR;
    U4S.DmaLen = USART4_RX_DATA_MAX - temp1;
    if(U4S.DmaLen == 0)return;
    if(U4S.RxLen != 0)return;
    if(U4S.RxBuffer == NULL)return;
    memcpy(U4S.RxBuffer, U4S_DMA_BUFFER, U4S.DmaLen);
    U4S.RxLen = U4S.DmaLen;
    memset(U4S_DMA_BUFFER, 0x00, USART4_RX_DATA_MAX);
    U4S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart4, (u8 *)U4S_DMA_BUFFER, USART4_RX_DATA_MAX);
#else
    u8 bytes = (u8)(huart4.Instance->DR);
    U4S.RxSate = STA_BUSY;
    if(U4T.Busy == 0) U4S.RxBuffer[U4S.DmaLen++] = bytes;
    if(U4S.DmaLen > USART4_RX_DATA_MAX)
    {
        U4S.DmaLen = 0;
        U4S.RxSate = STA_OVER;
        printf("\r\n USART4 SPILL");
    }
    U4T.Delay = 0;

#endif
}
void USART4_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart4, (uint8_t *)&Byte, 1, 0xffff);
}
void USART4_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart4, (uint8_t *)iStr, strlen(iStr), 0xffff);
}
void USART4_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart4, iPrt, Len, 0xffff);
}
void USART4_ReciveReset(void)
{
    U4S.RxLen = 0;
    memset(U4S.RxBuffer, 0, USART4_RX_DATA_MAX);
}
static void USART4_CallBackTIM()
{
#ifndef _UART4_DMA_
    if(U4S.RxSate == STA_BUSY)
    {
        if(++U4T.Delay > U4T.DelayMax)
        {
            U4S.RxSate = STA_END;
            U4S.RxLen = U4S.DmaLen;
            U4S.DmaLen = 0;
        }
    }
#endif
}
#endif

#ifdef _UART5_ENABLE_
//************************UART5********************************
USART_INFO_STRUCT U5S;
u8 U5S_RX_BUFFER[USART5_RX_DATA_MAX + 1];
#ifdef _UART5_DMA_
static char	U5S_DMA_BUFFER[USART5_RX_DATA_MAX + 1];
extern DMA_HandleTypeDef hdma_uart5_rx;
#else
static USART_DELAY_STRUCT U5T;
#endif
void USART5_InterruptCall(void)
{
#ifdef _UART5_DMA_
    if(__HAL_UART_GET_FLAG(&huart5, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart5);
    HAL_UART_DMAStop(&huart5);

    u32 temp1 = hdma_uart5_rx.Instance->NDTR;
    U5S.DmaLen = USART5_RX_DATA_MAX - temp1;
    if(U5S.DmaLen == 0)return;
    if(U5S.RxLen != 0)return;
    if(U5S.RxBuffer == NULL)return;
    memcpy(U5S.RxBuffer, U5S_DMA_BUFFER, U5S.DmaLen);
    U5S.RxLen = U5S.DmaLen;
    memset(U5S_DMA_BUFFER, 0x00, USART5_RX_DATA_MAX);
    U5S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart5, (u8 *)U5S_DMA_BUFFER, USART5_RX_DATA_MAX);
#else
    u8 bytes = (u8)(huart5.Instance->DR);
    U5S.RxSate = STA_BUSY;
    if(U5T.Busy == 0) U5S.RxBuffer[U5S.DmaLen++] = bytes;
    if(U5S.DmaLen > USART5_RX_DATA_MAX)
    {
        U5S.DmaLen = 0;
        U5S.RxSate = STA_OVER;
        printf("\r\n USART5 SPILL");
    }
    U5T.Delay = 0;
#endif
}
void USART5_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart5, (uint8_t *)&Byte, 1, 0xffff);
}
void USART5_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart5, (uint8_t *)iStr, strlen(iStr), 0xffff);
}
void USART5_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart5, iPrt, Len, 0xffff);
}
void USART5_ReciveReset(void)
{
    U5S.RxLen = 0;
    memset(U5S.RxBuffer, 0, USART5_RX_DATA_MAX);
}
static void USART5_CallBackTIM()
{
#ifndef _UART5_DMA_
    if(U5S.RxSate == STA_BUSY)
    {
        if(++U5T.Delay > U5T.DelayMax)
        {
            U5S.RxSate = STA_END;
            U5S.RxLen = U5S.DmaLen;
            U5S.DmaLen = 0;
        }
    }
#endif
}
#endif

#ifdef _UART6_ENABLE_
//************************UART6********************************
USART_INFO_STRUCT U6S;
u8 U6S_RX_BUFFER[USART6_RX_DATA_MAX + 1];
#ifdef _UART6_DMA_
static char	U6S_DMA_BUFFER[USART6_RX_DATA_MAX + 1];
extern DMA_HandleTypeDef hdma_usart6_rx;
#else
static USART_DELAY_STRUCT U6T;
#endif
void USART6_InterruptCall(void)
{
#ifdef _UART6_DMA_
    if(__HAL_UART_GET_FLAG(&huart6, UART_FLAG_IDLE) != SET)return;
    __HAL_UART_CLEAR_IDLEFLAG(&huart6);
    HAL_UART_DMAStop(&huart6);
    u32 temp1 = hdma_usart6_rx.Instance->NDTR;
    U6S.DmaLen = USART6_RX_DATA_MAX - temp1;
    if(U6S.DmaLen == 0)return;
    if(U6S.RxLen != 0)return;
    if(U6S.RxBuffer == NULL)return;
    memcpy(U6S.RxBuffer, U6S_DMA_BUFFER, U6S.DmaLen);
    U6S.RxLen = U6S.DmaLen;
    memset(U6S_DMA_BUFFER, 0x00, USART6_RX_DATA_MAX);
    U6S.DmaLen = 0;
    HAL_UART_Receive_DMA(&huart6, (u8 *)U6S_DMA_BUFFER, USART6_RX_DATA_MAX);
#else
    u8 bytes = (u8)(huart6.Instance->DR);
    U6S.RxSate = STA_BUSY;
    if(U6T.Busy == 0) U6S.RxBuffer[U6S.DmaLen++] = bytes;
    if(U6S.DmaLen > USART6_RX_DATA_MAX)
    {
        U6S.DmaLen = 0;
        U6S.RxSate = STA_OVER;
        printf("\r\n USART6 SPILL");
    }
    U6T.Delay = 0;
#endif
}
void USART6_SendByte(u8 Byte)
{
    HAL_UART_Transmit(&huart6, (uint8_t *)&Byte, 1, 100);
}
void USART6_SendString(char *iStr)
{
    HAL_UART_Transmit(&huart6, (uint8_t *)iStr, strlen(iStr), 0xffff);
}
void USART6_SendArray(u8 *iPrt, u16 Len)
{
    HAL_UART_Transmit(&huart6, iPrt, Len, 0xffff);
}
void USART6_ReciveReset(void)
{
    U6S.RxLen = 0;
    memset(U6S.RxBuffer, 0, USART6_RX_DATA_MAX);
}
static void USART6_CallBackTIM()
{
#ifndef _UART6_DMA_
    if(U6S.RxSate == STA_BUSY)
    {
        if(++U6T.Delay > U6T.DelayMax)
        {
            U6S.RxSate = STA_END;
            U6S.RxLen = U6S.DmaLen;
            U6S.DmaLen = 0;
        }
    }
#endif
}
#endif


void USER_UART_INIT(void)
{
    //************************UART1********************************
#ifdef _UART1_ENABLE_
    U1S.RxBuffer = U1S_RX_BUFFER;
    U1S.SendByte = USART1_SendByte;
    U1S.SendString = USART1_SendString;
    U1S.SendArray = USART1_SendArray;
    U1S.ReciveReset = USART1_ReciveReset;
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
	
#ifdef _UART1_DMA_
    HAL_UART_Receive_DMA(&huart1, (u8 *)U1S_DMA_BUFFER, 2);
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
    __HAL_UART_CLEAR_IDLEFLAG(&huart1);
#else
    U1T.DelayMax = 2;
#endif
#endif

#ifdef _UART2_ENABLE_
    //************************UART2********************************
    U2S.RxBuffer = (char *)U2S_RX_BUFFER;
    U2S.SendByte = USART2_SendByte;
    U2S.SendString = USART2_SendString;
    U2S.SendArray = USART2_SendArray;
    U2S.ReciveReset = USART2_ReciveReset;
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
		
#ifdef 	_UART2_DMA_
//    HAL_UART_Receive_DMA(&huart2, (u8 *)U2S_DMA_BUFFER, 2);
		HAL_UART_Receive_DMA(&huart2, (u8 *)U2S_DMA_BUFFER, USART2_RX_DATA_MAX);
    __HAL_UART_CLEAR_IDLEFLAG(&huart2);
    __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
#else
    U2T.DelayMax = 2;
#endif

#endif

#ifdef _UART3_ENABLE_
    //************************UART3********************************
    U3S.RxBuffer = (char *)U3S_RX_BUFFER;
    U3S.SendByte = USART3_SendByte;
    U3S.SendString = USART3_SendString;
    U3S.SendArray = USART3_SendArray;
    U3S.ReciveReset = USART3_ReciveReset;


    __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
#ifdef _UART3_DMA_
    HAL_UART_Receive_DMA(&huart3, (u8 *)U3S_DMA_BUFFER, 2);
    __HAL_UART_CLEAR_IDLEFLAG(&huart3);
    __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
#else
    U3T.DelayMax = 500;
#endif

#endif

#ifdef _UART4_ENABLE_
    //************************UART4********************************
    U4S.RxBuffer = (char *)U4S_RX_BUFFER;
    U4S.SendByte = USART4_SendByte;
    U4S.SendString = USART4_SendString;
    U4S.SendArray = USART4_SendArray;
    U4S.ReciveReset = USART4_ReciveReset;

    __HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);
#ifdef _UART4_DMA_
    HAL_UART_Receive_DMA(&huart4, (u8 *)U4S_DMA_BUFFER, 2);
    __HAL_UART_CLEAR_IDLEFLAG(&huart4);
    __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
#else
    U4T.DelayMax = 50;
#endif
#endif

#ifdef _UART5_ENABLE_
    //************************UART5********************************
    U5S.RxBuffer = (char *)U5S_RX_BUFFER;
    U5S.SendByte = USART5_SendByte;
    U5S.SendString = USART5_SendString;
    U5S.SendArray = USART5_SendArray;
    U5S.ReciveReset = USART5_ReciveReset;


    __HAL_UART_ENABLE_IT(&huart5, UART_IT_RXNE);
#ifdef _UART5_DMA_
    HAL_UART_Receive_DMA(&huart5, (u8 *)U5S_DMA_BUFFER, 2);
    __HAL_UART_CLEAR_IDLEFLAG(&huart5);
    __HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE);
#else
    U5T.DelayMax = 500;
#endif
#endif

#ifdef _UART6_ENABLE_
    //************************UART6********************************
    U6S.RxBuffer = (char *)U6S_RX_BUFFER;
    U6S.SendByte = USART6_SendByte;
    U6S.SendString = USART6_SendString;
    U6S.SendArray = USART6_SendArray;
    U6S.ReciveReset = USART6_ReciveReset;


    __HAL_UART_ENABLE_IT(&huart6, UART_IT_RXNE);
#ifdef _UART6_DMA_
    HAL_UART_Receive_DMA(&huart6, (u8 *)U6S_DMA_BUFFER, 2);
    __HAL_UART_CLEAR_IDLEFLAG(&huart6);
    __HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
#else
    U6T.DelayMax = 500;
#endif
#endif
}
//*******************************************************************
void UART_CallBackTIM(void)
{
#ifdef _UART1_ENABLE_
    USART1_CallBackTIM();
#endif
#ifdef _UART2_ENABLE_
    USART2_CallBackTIM();
#endif
#ifdef _UART3_ENABLE_
    USART3_CallBackTIM();
#endif
#ifdef _UART4_ENABLE_
    USART4_CallBackTIM();
#endif
#ifdef _UART5_ENABLE_
    USART5_CallBackTIM();
#endif
#ifdef _UART6_ENABLE_
    USART6_CallBackTIM();
#endif
}
/******************* (C) COPYRIGHT 2015 Saiyinsi Tech.,RD-Team *****END OF FILE************/
